<template>
  <div>
    <van-nav-bar
      class="page-nav-bar"
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>
    <input type="file" ref="fileRef" hidden @change="onFileChange" />
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image class="avater" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="呢称"
      :value="user.name"
      is-link
      @click="isShowUpdateName = true"
    ></van-cell>
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isShowUpdateGender = true"
    ></van-cell>
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isShowUpdateBirthday = true"
    ></van-cell>
    <!-- 修改昵称 -->
    <van-popup
      v-model="isShowUpdateName"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name
        @close="isShowUpdateName = false"
        v-model="user.name"
      ></update-name>
    </van-popup>
    <!-- 修改性别 -->
    <van-popup v-model="isShowUpdateGender" position="bottom">
      <update-gender
        @close="isShowUpdateGender = false"
        v-model="user.gender"
      ></update-gender>
    </van-popup>
    <!-- 修改生日 -->
    <van-popup v-model="isShowUpdateBirthday" position="bottom">
      <update-birthday
        @close="isShowUpdateBirthday = false"
        v-model="user.birthday"
      ></update-birthday>
    </van-popup>
    <!-- 修改头像 -->
    <van-popup
      v-model="isShowUpdatePhoto"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        @close="isShowUpdatePhoto = false"
        :img="img"
        @update-photo="user.photo = $event"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo'
export default {
  name: 'UserProfile',
  props: [],
  components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
  data () {
    return {
      isShowUpdatePhoto: false,
      img: null,
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthday: false,
      imgPath: ''
    }
  },
  async created () {
    const { data: { data } } = await getUserProfile()
    this.user = data
  },
  mounted () {},
  methods: {
    onFileChange (e) {
      this.img = URL.createObjectURL(e.target.files[0])
      this.isShowUpdatePhoto = true
      this.$refs.fileRef.value = ''
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
.avater {
  width: 60px;
  height: 60px;
}
.van-popup {
  background-color: #f5f7f9;
}
</style>
